/**
 * Created by Administrator on 2017/2/22.
 */
var qiniu = require("qiniu");
//需要填写你的 Access Key 和 Secret Key
qiniu.conf.ACCESS_KEY = 'cpk5v-oLpfSjxQxCASACHSVDP7Tcrm_qoBELpzUU';
qiniu.conf.SECRET_KEY = 'GQGQufV2ehzQHnXdiMgN7s7FM5aSjZCNz4uELOQV';
//要上传的空间
var bucket = 'anxiaomo';
//上传到七牛后保存的文件名
//构建上传策略函数
var qn={
    uptoken:function (key) {
        var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
        return putPolicy.token();
    },
    uploadFile:function (uptoken, key, localFile) {
        var extra = new qiniu.io.PutExtra();
        qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) {
            if(!err) {
                // 上传成功， 处理返回值
                console.log(ret.hash, ret.key, ret.persistentId);
            } else {
                // 上传失败， 处理返回代码
                console.log(err);
            }
        });
    },
    remove:function (req,path,fn) {
        var client = new qiniu.rs.Client();
        //你要测试的空间， 并且这个key在你空间中存在

        var path=req.query.path.split("?")[0].split(".com/")[1];
        //删除资源
        client.remove(bucket, path, fn);
    },
    policy:function () {
        return new qiniu.rs.GetPolicy();
    }
};


module.exports=qn;